package com.example.person.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.metadata.style.WriteFont;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import com.alibaba.excel.write.style.column.SimpleColumnWidthStyleStrategy;
import com.example.person.common.JsonResult;
import com.example.person.entity.PersonDTO;
import com.example.person.entity.PersonExcelVO;
import com.example.person.entity.PersonVO;
import com.example.person.lister.PersonReadLister;
import com.example.person.service.IPersonService;
import com.example.person.utils.IdUtils;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sytsnb@gmail.com
 * @date 2023 2023/3/10 16:38
 */
@Api(tags = "01. 人员信息基本管理--2")
@RestController
@RequestMapping("/person")
public class PersonController {

    @Autowired
    private IPersonService personService;


    /**
     * 新增人员基本信息
     */
    @ApiOperation("新增人员信息")
    @ApiOperationSupport(order = 1)
    @PostMapping(value = "/addNew")
    public JsonResult add(@RequestBody PersonDTO personDTO) {
        personService.addNew(personDTO);
        return JsonResult.ok("添加用户成功");
    }

    /**
     * 更新人员基本信息
     */
    @ApiOperation("更新人员信息")
    @ApiOperationSupport(order = 2)
    @PostMapping(value = "/update/{id:[0-9]+}")
    public JsonResult update(@PathVariable String id, @RequestBody PersonDTO personDTO) {
        personService.updateById(id, personDTO);
        return JsonResult.ok("更新用户成功");
    }

    /**
     * 删除人员基本信息
     */
//    @ApiOperation("删除人员信息")
//    @ApiOperationSupport(order = 3)
//    @PostMapping(value = "/delete/{id}")
//    public JsonResult deleteById(@PathVariable String id) {
//        personService.deleteById(id);
//        return JsonResult.ok("删除用户成功");
//    }

    /**
     * 批量删除人员基本信息
     */
    @ApiOperation("批量删除人员信息")
    @ApiOperationSupport(order = 3)
    @PostMapping(value = "/delete/{ids}")
    public JsonResult deleteByIds(@PathVariable String[] ids) {
        personService.deleteByIds(ids);
        return JsonResult.ok("批量删除用户成功");
    }

    /**
     * 查询人员信息列表
     *
     * @return
     */

    @ApiOperation("查询人员信息列表")
    @ApiOperationSupport(order = 4)
    @GetMapping("/list")
    public List list() {
//        startPage();
        List<PersonVO> list = personService.selectList();
        return list;
    }

    /**
     * 查询人员信息列表byPerson
     *
     * @param personDTO
     * @return
     */
    @ApiOperation("根据personDTO查询人员信息列表")
    @ApiOperationSupport(order = 5)
    @PostMapping("/list")
    public List listByPersonDTO(PersonDTO personDTO) {
        List<PersonVO> list = personService.selectList(personDTO);
        return list;
    }

    /**
     * 下载Excel模板
     *
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "下载Excel模板")
    @ApiOperationSupport(order = 14)
    @GetMapping("/downloadExcelTemplate")
    public void downloadExcelTemplate(HttpServletResponse response) throws IOException {
        personService.downloadExcelTemplate(response);
//        System.out.println("response = " + response);
    }

    /**
     * 导出Excel表格
     *
     * @param response
     * @param ids
     * @throws IOException
     */
    @ApiOperation(value = "导出Excel表格")
    @ApiOperationSupport(order = 14)
    @GetMapping("/exportExcel/{ids}")
    public void exportExcel(HttpServletResponse response, @PathVariable String[] ids) throws IOException {
        personService.exportExcel(response, ids);
    }

    /**
     * 全部导出Excel表格
     *
     * @param response
     * @throws IOException
     */
    @ApiOperation(value = "全部导出Excel表格")
    @ApiOperationSupport(order = 15)
    @GetMapping("/exportExcelAll")
    public void exportExcelAll(HttpServletResponse response) throws IOException {
        String[] ids = {};
        System.out.println("ids.length = " + ids.length);
        personService.exportExcel(response, ids);
    }

    /**
     * 导入Excel表格数据
     *
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation(value = "导入Excel表格数据")
    @ApiOperationSupport(order = 16)
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("excelFile") MultipartFile file) throws IOException {

        personService.uploadExcel(file);

        return JsonResult.ok("上传成功");
    }
}
